//
//  ViewController.m
//  HaoZhouDao
//
//  Created by quan on 15/11/16.
//  Copyright © 2015年 quan. All rights reserved.
//

#import "HQViewController.h"
#import "HQZhou.h"
#import "HQCollectionViewCell.h"
#import "UIView+Extension.h"
#import "HQDetailViewController.h"
#import "HQNavigationController.h"
#import "UMSocial.h"
#import "UIButton+Extension.h"

#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height == 568) ? YES : NO)
#define IS_IPhone6 (667 == [[UIScreen mainScreen] bounds].size.height ? YES : NO)
#define IS_IPhone6plus (736 == [[UIScreen mainScreen] bounds].size.height ? YES : NO)



@interface HQViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UMSocialUIDelegate>

@property (nonatomic, assign) BOOL bannerIsVisible;

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (nonatomic, strong) NSArray *zhous;

@end

static NSString *identifier = @"identifier";

@implementation HQViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"米粥信息";
    
    //左导航按钮
    UIButton *leftButton = [UIButton buttonWithImage:@"nav_button" title:@"菜单" font:15 setColorRed:0.798 green:0.613 blue:0.392];
    [leftButton addTarget:self action:@selector(presentLeftMenuViewController:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    
    //右导航按钮
    UIButton *rightButton = [UIButton buttonWithImage:@"nav_share_button" title:nil font:15 setColorRed:0.798 green:0.613 blue:0.392];
    [rightButton addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    //准备UI
    [self prepareForUI];


}

//友盟分享
- (void)share {
//    NSLog(@"点击了分享");
    NSArray *snsNames = [NSArray arrayWithObjects:UMShareToSina, UMShareToTencent,UMShareToRenren,UMShareToDouban,UMShareToEmail,UMShareToFacebook,UMShareToQzone,UMShareToWechatSession,UMShareToWechatTimeline, nil];
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"564f09c667e58e35d5003657"
                                      shareText:@"这个应用很实用,平常喜欢煲粥的朋友可以下载哦,不用连网"
                                  shareImage:[UIImage imageNamed:@"blue_sky"]
                        shareToSnsNames:snsNames
                                     delegate:self];
    
}

//准备UI
- (void)prepareForUI{
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"common_bg"]];
    self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"common_bg"]];
    
    //注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"HQCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:identifier];
    
    //设定参数
    UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake(150, 170);
    layout.sectionInset = UIEdgeInsetsMake(10, 5, 10, 5);
    
    if (IS_IPhone6) {
        layout.itemSize = CGSizeMake(170, 190);
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    }
    else if (IS_IPhone6plus) {
        layout.itemSize = CGSizeMake(190, 210);
        layout.sectionInset = UIEdgeInsetsMake(10, 15, 10, 15);
    }

//    layout.itemSize = CGSizeMake(150, 170);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 10;
    
    
    //指定参数
    self.collectionView.collectionViewLayout = layout;
    
    //指定代理
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
}

//数据源方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.zhous.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HQCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    HQZhou *zhou = self.zhous[indexPath.item];
    cell.zhou = zhou;
    
    cell.transform = CGAffineTransformScale(cell.transform, 0.5, 0.5);
    
    [UIView animateWithDuration:0.5 animations:^{
        cell.transform = CGAffineTransformIdentity;
    }];

//    NSLog(@"调用了数据源方法");
    return cell;
}

//collection代理方法

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HQDetailViewController *detailVC = [[HQDetailViewController alloc] init];
    HQZhou *zhou = self.zhous[indexPath.item];
    detailVC.zhou = zhou;
    detailVC.fromVC = @"mainVC";
    HQNavigationController *nav = [[HQNavigationController alloc] initWithRootViewController:detailVC];
    
    [self presentViewController:nav animated:true completion:nil];
    
}


//懒加载
- (NSArray *)zhous{
    if (_zhous == nil) {
        _zhous = [HQZhou zhous];
    }
    return _zhous;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
